package com.ctshk.app.user.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.user.dto.UserCollectionHeadDTO;
import com.ctshk.rpc.user.dto.UserCollectionListDTO;
import com.ctshk.rpc.user.req.UserCollectionBatchCancelReq;
import com.ctshk.rpc.user.req.UserCollectionPageReq;
import com.ctshk.rpc.user.req.UserCollectionReq;
import com.ctshk.rpc.user.service.IUserCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  用户收藏前端控制器
 * </p>
 *
 * @author 李新科
 * @date 2021/5/17 17:06
 */
@Api(value="用户收藏", tags="用户收藏")
@RestController
@RequestMapping("/userCollection")
public class UserCollectionController extends AppBaseController {

    @DubboReference
    private IUserCollectionService userCollectionService;

    @ApiOperation(value="收藏列表", notes="收藏列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "收藏列表", operDesc = "收藏列表")
    @PostMapping("/list")
    public Result<PageResponse<UserCollectionListDTO>> queryPage(@RequestBody @Valid UserCollectionPageReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userCollectionService.queryPage(req);
    }

    @ApiOperation(value="是否已收藏", notes="是否已收藏")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "是否已收藏", operDesc = "是否已收藏")
    @PostMapping(value = "/isCollect/{productId}")
    public Result<Boolean> isCollect(@PathVariable @NotNull(message = "产品Id不能为空") Long productId) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userCollectionService.isCollect(productId,currentUser.getId());
    }

    @ApiOperation(value="取消收藏并返回收藏列表", notes="取消收藏并返回收藏列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "取消收藏并返回收藏列表", operDesc = "取消收藏并返回收藏列表")
    @PostMapping("/cancel/{id}")
    public Result<List<Object>> cancel(@PathVariable @NotNull(message = "id不能为空")
                         @ApiParam(name="产品详情id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userCollectionService.delete(id,currentUser.getId());
    }

    @ApiOperation(value="批量删除收藏", notes="批量删除收藏")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "批量删除收藏", operDesc = "批量删除收藏")
    @PostMapping("/batchCancel")
    public Result batchCancel(@RequestBody @Valid UserCollectionBatchCancelReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userCollectionService.batchDelete(req);
    }

    @ApiOperation(value="我的收藏顶部数据", notes="我的收藏顶部数据")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "我的收藏顶部数据", operDesc = "我的收藏顶部数据")
    @PostMapping("/queryHead")
    public Result<UserCollectionHeadDTO> queryHead() throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userCollectionService.queryHead(currentUser.getId());
    }

    @ApiOperation(value="已收藏列表id数组", notes="已收藏列表id数组")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "已收藏列表id数组", operDesc = "已收藏列表id数组")
    @GetMapping("/collectedlist")
    public Result<List<Object>> collectedlist() throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userCollectionService.collectedlist(currentUser.getId());
    }

    @ApiOperation(value="新增收藏并返回收藏列表", notes="新增收藏并返回收藏列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.ADD, operTitle = "新增收藏并返回收藏列表", operDesc = "新增收藏并返回收藏列表")
    @GetMapping("/add/{productId}")
    public Result<List<Object>> addReturnIdList(@PathVariable @NotNull(message = "详情id不能为空") Long productId) throws Exception{
        TokenUser currentUser = getCurrentUser();
        UserCollectionReq req = new UserCollectionReq();
        req.setUserId(currentUser.getId());
        req.setDetailId(productId);
        return userCollectionService.add(req);
    }
}
